<template>
    <div>
        <div class="loginBg" v-if="showFlag">

            <img src="../../assets/bgImg.png" class="bgImg" alt="">

            <div class="infoBox">
                <div>
                    <span>账号</span>
                    <input type="number" v-model="userTel" placeholder="请输入手机号">
                </div>
                <div>
                    <span>密码</span>
                    <input type="number" v-model="userPass" placeholder="请输入登录密码">
                </div>
                <div class="loginBtn" @click="loginHandle">登录</div>
            </div>
        </div>
    </div>
</template>

<script>


import { isWXBrowser, getWechatCode } from "../../utils/common";
import { login, advLogin } from "../../network/mobile";

export default {
    name: 'HomeR',
    data() {
        return {
            code: '',
            userTel: '',
            userPass: '',
            showFlag: false
        }
    },
    mounted() {
        this.setTitle()
    },
    methods: {

        loginHandle() {
            if (this.userTel == '') {
                this.$message.warning('请输入账号')
                return
            }

            if (this.userPass == '') {
                this.$message.warning('请输入登录密码')
                return
            }

            advLogin({
                userTel: this.userTel,
                userPass: this.userPass,
            }).then(res => {
                if (res.flag) {
                    if (res.data.flag == 'error') {
                        this.$message.warning(res.data.msg)
                        return
                    }
                    localStorage.user = JSON.stringify(res.data.user)
                    localStorage.userId = res.data.user.userId
                    localStorage.userTag = res.data.user.userTag
                    localStorage.userPassword = this.userPass
                    console.log(res);
                    if (res.data.user.userTag == -2) {
                        // 推广员登录
                        this.$router.replace('/index')
                    } else if (res.data.user.userTag == -3) {
                        // 施工员登录
                        this.$router.replace('/advertising')
                    }
                }
            })
        },

        login() {
            login({
                code: this.code
            }).then(res => {
                if (res.flag) {
                    console.log(res);
                    if (res.data.flag == 'error') {
                        alert('登录异常，正在重试')
                        this.login()
                    } else {
                        if (res.data.flag == 'success') {
                            localStorage.user = JSON.stringify(res.data.user)
                            localStorage.userId = res.data.user.userId
                            localStorage.openId = res.data.user.openId
                            localStorage.userTag = 0
                        } else {
                            localStorage.openId = res.data.openId
                            localStorage.userTag = 0
                        }
                        this.$router.replace('/index')
                    }
                }
            })
        },
        setTitle() {
            document.title = '登录'
            const i = document.createElement('iframe')
            i.src = '/favicon.ico'
            i.style.display = 'none'
            i.onload = function () {
                setTimeout(function () {
                    i.remove()
                }, 9)
            }
            document.body.appendChild(i)
        },
    },
    created() {

        console.log(this.$router);
        let saleManId = this.$router.history.current.query.saleManId
        if (saleManId) {
            localStorage.saleManId = saleManId
            if (localStorage.user && localStorage.userTag == 0) {
                this.$router.push('/index')
            } else {
                if (isWXBrowser) {
                    this.code = getWechatCode();
                    if (this.code) {
                        this.login()
                    }
                }
            }
        } else {
            this.showFlag = true
            if (localStorage.user && localStorage.userTag != 0) {
                if (localStorage.userTag == -3) {
                    this.$router.replace('/advertising')
                } else {
                    this.$router.replace('/index')
                }
            }
        }


    },
}







</script>

<style lang='less' scoped>
.loginBg {
    width: 100%;
    height: 100vh;
    // background-color: rgb(91, 222, 255);
    background: linear-gradient(156deg, #1bbef067, #0091f141);
    overflow: hidden;
}


.infoBox {
    position: absolute;
    top: 40vh;
    box-sizing: border-box;
    padding: 0 32px 0 48px;
    width: 100%;
}

.infoBox>div {
    padding: 30px 0;
    width: 100%;
    border-bottom: 2px solid #F2F2F2;
    font-family: Source Han Sans CN;
    font-weight: 400;
    font-size: 32px;
    color: #333333;
}

.infoBox>div>input {
    width: 80%;
    // background-color: antiquewhite;
    border: none;
    background-color: transparent;
}

.infoBox>div>span {
    margin-right: 30px;
}

.loginBtn {
    margin-top: 64px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 654px;
    background: #0A6DEC;
    border-radius: 10px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    font-size: 28px;
    color: #FFFFFF !important;
    border: none !important;
    cursor: pointer;
}

.bgImg {
    display: block;
    margin: 10vh auto;
    width: 356px;
    height: 346px;
}
</style>